package mfuny.ggongal.admin.advertisers;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.debug.Debug;
import mfuny.ggongal.admin.memo.MemoDao;
import mfuny.ggongal.admin.page.PageDao;
import mfuny.ggongal.admin.page.Pager;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class AdvertiserService {
	SqlMapClient sqlMapper;
	
	ModelAndView mav;
	AdvertiserDao advDao;
	Pager page;
	PageDao pageClass;
	HashMap<String,Object> map;
	List<AdvertiserDao> resultList;
	List<MemoDao> memoList;
	
	int pageSize = 10;
	int pageGroupSize = 10;
	
	public AdvertiserService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
		advDao = new AdvertiserDao();
		page = new Pager();
		pageClass = new PageDao();
		resultList = new ArrayList<AdvertiserDao>();
		memoList = new ArrayList<MemoDao>();
		map = new HashMap<String,Object>();
	}
	
	public ModelAndView regAdvForm() throws Exception {
		mav.clear( );
		mav.setViewName( "/advertisers/regAdvForm.ga" );
		return mav;
	}
	
	public void regAdv( AdvertiserDao advDao ) throws Exception {
		advDao.setCi_img_capacity(advDao.getCi_file().getSize());
		advDao.setCi_img_oriname(advDao.getCi_file().getOriginalFilename());
		advDao.setCi_img_savname(System.currentTimeMillis() + "_" + new Random().nextInt(100) + "_" + advDao.getId() + ".jpg");
		advDao.setRegdate( new Timestamp( System.currentTimeMillis( )) );
		advDao.getCi_file().transferTo(new File(Server.filePath + advDao.getCi_img_savname()));
		sqlMapper.update( "adv_insert", advDao );
	}
	
	public ModelAndView advList( String pageNum ) throws Exception {
		mav.clear( );
		pageClass = page.getPageClass( "advList", pageNum, pageSize, pageGroupSize, "adv_pageList", "adv_countAll" );
		mav.setViewName( "/advertisers/advList.ga" );
		mav.addObject( "pageClass", pageClass );
		return mav;
	}
	
	public void updateAdvStatus( AdvertiserDao advDao ) throws Exception {
		sqlMapper.update( "adv_updateStatus", advDao );
	}
	
	@SuppressWarnings( "unchecked" )
	public ModelAndView advMemoPopup( MemoDao memoDao ) throws Exception {
		memoList = sqlMapper.queryForList( "memo_selectOne", memoDao );
		mav.clear( );
		mav.setViewName( "/advertisers/advMemoPopup" );
		mav.addObject( "memoList", memoList );
		mav.addObject( "memoClass", memoDao );
		mav.addObject( "listSize", memoList.size() );
		return mav;
	}
	
	public ModelAndView regAdvMemo( MemoDao memoDao ) throws Exception {
		memoDao.setRegdate( new Timestamp( System.currentTimeMillis( )) );
		sqlMapper.update( "memo_insert", memoDao );
		mav.clear( );
		mav.setViewName( "/popupClose" );
		return mav;
	}
	
	public ModelAndView modifyAdvForm( String id ) throws Exception {
		advDao = (AdvertiserDao)sqlMapper.queryForObject( "adv_selectId", id );
		advDao.setCoopendate( advDao.getCoopendate( ).substring( 0, 10 ) );
		mav.clear( );
		mav.setViewName( "/advertisers/modifyAdvForm.ga" );
		mav.addObject( "resultClass", advDao );
		return mav;
	}
	
	public void modifyAdv( AdvertiserDao advDao ) throws Exception {
		try{
			sqlMapper.startTransaction();
			AdvertiserDao beforeDao = (AdvertiserDao) sqlMapper.queryForObject("adv_selectId", advDao.getId());
			
			if(advDao.getCi_file() == null) {
				advDao.setCi_img_oriname("");
				advDao.setCi_img_savname("");
				advDao.setCi_img_capacity(0);
			} else {
				advDao.setCi_img_oriname(advDao.getCi_file().getOriginalFilename());
				advDao.setCi_img_savname(System.currentTimeMillis() + "_" + new Random().nextInt(100) + "_" + advDao.getId() + ".jpg");
				advDao.setCi_img_capacity(advDao.getCi_file().getSize());
				advDao.getCi_file().transferTo(new File(Server.filePath + advDao.getCi_img_savname()));
			}
			
			int result = sqlMapper.update( "adv_update", advDao );
			
			if(result > 0) {
				new File(Server.filePath + beforeDao.getCi_img_savname()).delete();
			} else {
				throw new Exception();
			}
			
			sqlMapper.commitTransaction();
		}catch(Exception e){
			Debug.toDebugFile(e, "modifyAdv");
			e.printStackTrace();
		}finally{
			sqlMapper.endTransaction();
		}
	}
	
	public ModelAndView detailAdv( String id ) throws Exception {
		advDao = (AdvertiserDao)sqlMapper.queryForObject( "adv_selectId", id );
		advDao.setCoopendate( advDao.getCoopendate( ).substring( 0, 10 ) );
		mav.clear( );
		mav.setViewName( "/advertisers/detailAdv.ga" );
		mav.addObject( "resultClass", advDao );
		return mav;
	}
	
	public ModelAndView modifyAdvPassForm( String id ) throws Exception {
		String pass = (String)sqlMapper.queryForObject( "adv_selectPass", id );
		mav.clear( );
		mav.setViewName( "/advertisers/modifyAdvPassForm.ga" );
		mav.addObject( "id", id );
		mav.addObject( "pass", pass );
		return mav;
	}
	
	public void modifyAdvPass( String id, String newpass ) throws Exception {
		map.clear( );
		map.put( "id", id );
		map.put( "newpass", newpass );
		sqlMapper.update( "adv_updatePass", map );
	}
	
	@SuppressWarnings( "unchecked" )
	public ModelAndView advOutForm() throws Exception {
		resultList = sqlMapper.queryForList( "adv_selectNoOut", null );
		mav.clear( );
		mav.setViewName( "/advertisers/advOutForm.ga" );
		mav.addObject( "resultList", resultList );
		return mav;
	}
	
	public ModelAndView advOutCheck( ) throws Exception {
		return null;
	}
	
	public void advOut( AdvertiserDao advDao ) throws Exception {
		advDao.setStatus( "out" );
		sqlMapper.update( "adv_updateStatus", advDao );
	}
}
